// pages/carDetails/carDetails.js


const app = getApp()
const util = require('../../utils/util')


Page({
  onPullDownRefresh() {

    wx.showLoading({
      title: '正在处理',
      mask: true
    })
    this.address()
    wx.stopPullDownRefresh()
  },
  /**
   * 页面的初始数据
   */
  data: {
    express: true,
    url: '',
    allInfo: {},
    product: [],
    curArr: [],
    address: {},
    adresState: true,
    proimgUrl: '',
    quota: Number,
    seleItem: [
   
    ],
    msg: '',
    peisong:'',
    payType: [
      {
        name: '微信支付',
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */

  // select (e) {
  //     this.setData({
  //         express: !this.data.express
  //     })
  // },


  address() {
    let that = this
    wx.request({
      url: that.data.url + 'ny_flow_cart.php?action=get_consignee', //产品
      data: {
        openid: app.globalData.opid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        console.log(res)
        
        let adresState = that.data.adresState;

        if (res.data.address_ok == 0) {
          that.setData({
            adresState: false
          })
        } else {
          that.setData({
            address: res.data
          })
          wx.hideLoading()
        }
      }
    })
  },
  toSelect() {
    wx.navigateTo({
      url: './../get-place/get-place'
    })
  },
  Kdselect(e) {
    var that = this;
    let idx = e.currentTarget.dataset.index
    let data = this.data.seleItem
    let spId = e.currentTarget.dataset.id

    data.map((item) => {
      item.radio = false
    })

    data[idx].radio = !data[idx].radio;
    console.log(data[idx].shipping_id  )
    
    wx.request({
      url: app.globalData.url +'ny_flow_cart.php?action=feiyun',
      data:{
        openid: app.globalData.opid,
        shipping_id: data[idx].shipping_id  
      },
      success: function (res) {
        console.log(res)
        that.setData({
          peisong: res.data.shipping_fee_formated
        })
      }
    })
    this.setData({
      seleItem: data,
      spId: spId,
      
    })

  },

  getKd: function () {
    let that = this
    wx.request({
      url: app.globalData.url + 'ny_flow_cart.php?action=get_shipping',
      data:{
        openid: app.globalData.opid
      },
      success: function (res) {
        console.log(res)
        that.setData({
          seleItem: res.data
        })
      }
    })
  },


  paySelect(e) {
    let idx = e.currentTarget.dataset.index
    let data = this.data.payType
    console.log(e)
    data.map((item) => {
      item.radio = false
    })

    data[idx].radio = !data[idx].radio

    this.setData({
      payType: data
    })

  },
  msgFn:function(e){
    this.setData({
      msg: e.detail.value
    })
   
  },
  suss() {
    wx.showLoading({
      title: '处理中',
      mask: true
    })
    let that = this;
    let data = this.data.seleItem
    data.map((item) => {
      item.radio = true
    })
    // if (!that.data.spId){
    //   wx.showToast({
    //     title: '请选择配送方式',
    //     icon:'none'
    //   })
    //   return false
    // }
    if (that.data.address.tel) {

      wx.request({
        url: that.data.url + 'ny_flow_pay.php?action=pre_wxpay', //支付
        data: {
          openid: app.globalData.opid,
          customer_msg: that.data.msg,
          shipping_id : 18,
          province: that.data.address.province,
          city: that.data.address.city,
          district: that.data.address.district,
          postscript: that.data.msg
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          wx.hideLoading()
          console.log(res)
          console.log('地址：',that.data.address.province, that.data.address.city, that.data.address.district)
          console.log('shipping_id:',that.data.spId)
          wx.requestPayment(
            {
              'timeStamp': res.data.timeStamp,
              'nonceStr': res.data.nonceStr,
              'package': res.data.package,
              'signType': 'MD5',
              'paySign': res.data.paySign,
              'success': function (res) {
                console.log(res)
                wx.showToast({
                  title: '支付成功',//这里打印出报名成功
                  icon: 'success',
                  duration: 1000
                })

                wx.navigateTo({
                  url: "../sc-sucess/sc-success"
                })
                wx.hideLoading()

              },
              'fail': function (res) {
                console.log(res)
                wx.showLoading({
                  title: '处理中',
                  mask: true
                })
                console.log()
                wx.navigateTo({
                  url: "../myOrder/myOrder"
                })
                wx.hideLoading()
              },
              'complete': function (res) { }
            })
        }
      })
    } else {
      wx.showToast({
        title: '请填写收货信息',
        icon: 'none',
        duration: 2000,
        mask: true
      })
    }
  },

  onLoad: function (options) {
    let that = this
    let proimgUrl = app.globalData.url.replace('mobile/', '')

    this.setData({
      url: app.globalData.url,
      proimgUrl: proimgUrl
    })

    that.address()
    that.getKd()
    wx.request({
      url: that.data.url + 'ny_flow_cart.php?action=get_checkout_info', //产品
      data: {
        openid: app.globalData.opid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        let arr = []
        let count = 0;
        console.log(res)

        for (var key in res.data.goods_list) {
          arr.push(res.data.goods_list[key])
        }

        that.setData({
          allInfo: res.data.total
        })

        arr = JSON.parse(JSON.stringify(arr))
        arr.pop()

        arr.map((item) => {
          count += parseInt(item.goods_number)
        })

        console.log(arr)
        that.setData({
          product: arr,
          count: count
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.request({
      url: 'https://rt.gzziyu.com//mobile/ny_flow_cart.php?action=gengxinzt',
      data:{
        openid:app.globalData.opid
      },
      success:function(res){
        console.log(res)
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})